<?php


namespace backend\controllers;


use common\cache\DictCache;
use common\helpers\commonApi;
use common\models\System;
use Yii;

/**
 * 设置
 * Class SystemController
 * @package backend\controllers
 */
class SystemController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = System::className();
    }

    /**
     * 基础设置
     * @return array|string
     */
    public function actionIndex()
    {
        $model = System::getModel();
        $withdraw_way = DictCache::get('withdraw_way');
        $dis_field = DictCache::get('dis_field');
        $model->withdraw_way = explode(',',$model->withdraw_way);
        return $this->render('',['model'=>$model,'withdraw_way'=>$withdraw_way,'dis_field'=>$dis_field]);
    }

    /**
     * 小程序信息设置
     * @return string
     */
    public function actionApplet()
    {
        $model = System::getModel();
        return $this->render('',['model'=>$model]);
    }

    /**
     * 页面主题色
     * @return string
     */
    public function actionThemeColor()
    {
        $model = System::getModel();
        return $this->render('',['model'=>$model]);
    }

    /**
     * 支付设置
     * @return string
     */
    public function actionPay()
    {
        $model = System::getModel();
        return $this->render('',['model'=>$model]);
    }

    /**
     * 配送费设置
     * @return string
     */
    public function actionDelivery()
    {
        $model = System::getModel();
        return $this->render('',['model'=>$model]);
    }
    /**
     * 统一保存
     * @return array
     */
    public function actionTran()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            if (!empty($post['withdraw_way'])){
                $post['withdraw_way'] = implode(',',$post['withdraw_way']);
            }
            $model = System::getModel();
            if (!$model){
                $model = new System();
            }
            $model->load($post,'');
            if ($model->validate()&&$model->save()){
                return commonApi::message('设置成功');
            }
            $msg = commonApi::getModelError($model);
            return commonApi::message($msg,false);
        }
    }
}